var htable;
$(document).ready(function() {
	htable = factory_table();
} );

//初始化并返回对象
function factory_table(){
	var table = $('#dynamic-table').DataTable( {		  
		"bProcessing": true,
		"ajax":{
            "url":"/admin/menu/getlist",
			"type":'get',
        },
		"columns":[
			{"data":"id"},
			{"data":"name"},
			{"data":"up_title"},
			{"data":"group"},
			{"data":"url"},
			{"data":"sort"},
			{
				"data":"icon",
				"render":function ( data, type, full, meta ) {
					var str = '<a href="javascript:;"><i class="fa '+data+'" ></i></a>';
				  	return str;
				},
			},
			{
				"data":"is_hidden",
				"render":function ( data, type, full, meta ) {
					if(1 == data){
						var str = '<a href="javascript:;"><i class="fa fa-times text-danger" ></i></a>';
					}else{
						var str = '<a href="javascript:;"><i class="fa fa-check text-success" ></i></a>';
					}
				  	return str;
				},
			},
			{"data":"status"},
			{
				"data":"id",
				"render": function ( data, type, full, meta ) {
				  	var str = '<a href="javascript:;" onclick="show_childgroups('+data+');" class="btn btn-info btn-xs">下级</a> ';
					str += '<a href="/admin/menu/edit.html?id='+data+'" class="btn btn-success btn-xs" data-ajax="ajax-auto" title="编辑菜单">编辑</a> ';
					str += '<a href="javascript:;" class="btn btn-danger btn-xs" onclick="delete_row('+data+')">删除</a>';
				  	return str;
				}},
		],
		"drawCallback": function( settings ) {
			ajax_auto();
		},
		language: {
			"sProcessing": "处理中...",
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sZeroRecords": "没有匹配结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
			"sInfoFiltered": "(由 _MAX_ 项结果过滤)",
			"sInfoPostFix": "",
			"sSearch": "搜索:",
			"sUrl": "",
			"sEmptyTable": "表中数据为空",
			"sLoadingRecords": "载入中...",
			"sInfoThousands": ",",
			"oPaginate": {
				"sFirst": "首页",
				"sPrevious": "上页",
				"sNext": "下页",
				"sLast": "末页"
			},
			"oAria": {
				"sSortAscending": ": 以升序排列此列",
				"sSortDescending": ": 以降序排列此列"
			}
    	}
    } );
	return table;
}

function show_childgroups(id){
	htable.ajax.url('/admin/menu/getlist/pid/'+id).load(function(e){
        if(e.breadcrumb){
            var nav = []
            var html;
            var n=e.breadcrumb.length;
            e.breadcrumb.forEach(function(v,k){
                if(k+1 == n){
                    html='<li class="active">'+v.name+'</li>'
                }else{
                    html='<li><a href="javascript:void(0);" onclick="show_childgroups('+v.id+')">'+v.name+'</a></li>'
                }
                nav.push(html);
            })
            }
        //console.log(nav.join(""));
       $('.breadcrumb').html('<li><a href="javascript:void(0);" onclick="show_childgroups(0)">菜单列表</a></li>'+nav.join(""));
    });	
}

function delete_row(id){
	layer.confirm('确定要删除吗？',function(index)
	{
		$.ajax({
		type:'POST',
		data:{'id':id},
		url:'/admin/menu/delete',
		success:function(data)
			{
				if(data.errno > 0)
				{
					layer.msg(data.errmsg,{icon:2,time: 3000}); 	  
				}else
				{
					htable.ajax.reload();
					layer.msg(data.data.info,{icon:1,time: 1000});
				}
			}					
		});	  
	})		
}

function edit_row(id){
	
}



